#include "SFTGA.h"

namespace SF
{
	SFTGAImg::SFTGAImg()
	{
		m_siWidth = 0;
		m_siHeight = 0;
		m_siBPP = 0;
		m_slImageSize = 0;
		m_bEnc = false;
		m_pucImage = NULL;
		m_pucPalette = NULL;
		m_pucData = NULL;
	}

	SFTGAImg::~SFTGAImg()
	{
		if(m_pucImage)
		{
			delete [] m_pucImage;
			m_pucImage = NULL;
		}

		if(m_pucPalette)
		{
			delete [] m_pucPalette;
			m_pucPalette = NULL;
		}

		if(m_pucData)
		{
			delete [] m_pucData;
			m_pucData = NULL;
		}
	}

	int SFTGAImg::Load(const char* szFilename)
	{
		using namespace std;
		ifstream fIn;
		unsigned long ulSize;
		int iRet;

		if(m_pucImage)
		{
			delete [] m_pucImage;
			m_pucImage=NULL;
		}

		if(m_pucPalette)
		{
			delete [] m_pucPalette;
			m_pucPalette=NULL;
		}

		fIn.open(szFilename,ios::binary);
		if(fIn==NULL)
		{
			return IMG_ERR_NO_FILE;
		}

		fIn.seekg(0,ios_base::end);
		ulSize=fIn.tellg();
		fIn.seekg(0,ios_base::beg);

		if(m_pucData)
		{
			delete [] m_pucData; 
		}
			
		m_pucData = new unsigned char[ulSize];
		if(m_pucData==NULL)
		{
			fIn.close();
			return IMG_ERR_MEM_FAIL;
		}

		fIn.read((char*)m_pucData,ulSize);
		fIn.close();

		iRet=ReadHeader();
		if(iRet!=IMG_OK)
		{
			return iRet;
		}
		
		switch(m_bEnc)
		{
		case 1:			 // Raw Indexed
			{
				if((m_slImageSize + 18 + m_pucData[0] + 768)>ulSize)
				{
					return IMG_ERR_BAD_FORMAT;
				}

				if(m_pucData[1] != 1)
				{
					return IMG_ERR_BAD_FORMAT;
				}

				iRet = LoadRawData();
				if(iRet!=IMG_OK)
				{
					return iRet;
				}

				iRet = LoadTgaPalette();
				if(iRet!=IMG_OK)
				{
					return iRet;
				}
					
				break;
			}
		case 2:			// Raw RGB
			{
				if((m_slImageSize + 18 + m_pucData[0]) > ulSize)
				{
					return IMG_ERR_BAD_FORMAT;
				}
					
				if(m_pucData[1]!=0)
				{
					return IMG_ERR_BAD_FORMAT;
				}
					
				iRet = LoadRawData();
				if(iRet!=IMG_OK)
				{
					return iRet;
				}
					
				BGRtoRGB();							// Convert to RGB
				break;
			}
		case 9:			// RLE Indexed
			{
				if(m_pucData[1] != 1)
				{
					return IMG_ERR_BAD_FORMAT;
				}
					
				// Load image data
				iRet = LoadTgaRLEData();
				if(iRet!=IMG_OK)
				{
					return iRet;
				}
					
				// Load palette
				iRet = LoadTgaPalette();
				if(iRet != IMG_OK)
				{
					return iRet;
				}
					
				break;
			}
		case 10:
			{
				// Double check image type field
				if( m_pucData[1] != 0)
				{
					return IMG_ERR_BAD_FORMAT;
				}

				// Load image data
				iRet = LoadTgaRLEData();
				if(iRet!=IMG_OK)
				{
					return iRet;
				}

				BGRtoRGB(); // Convert to RGB
				break;
			}
		}
		
		return 0;
	}

	int SFTGAImg::GetBPP()
	{
		return m_siBPP;
	}

	int SFTGAImg::GetWidth()
	{
		return m_siWidth;
	}

	int SFTGAImg::GetHeight()
	{
		return m_siHeight;
	}

	unsigned char* SFTGAImg::GetImg(void)
	{
		return m_pucImage;
	}

	bool SFTGAImg::DownSampling(void)
	{
		return true;
	}

	unsigned char* SFTGAImg::GetPalette(void)
	{
		return m_pucPalette;
	}

	int SFTGAImg::ReadHeader()
	{
		return 0;
	}

	int SFTGAImg::LoadRawData()
	{
		return 0;
	}

	int SFTGAImg::LoadTgaRLEData()
	{
		return 0;
	}

	int SFTGAImg::LoadTgaPalette()
	{
		return 0;
	}

	void SFTGAImg::BGRtoRGB(void)
	{

	}

	void SFTGAImg::FlipImg()
	{

	}

	unsigned char* SFTGAImg::GetPixel(int x, int y)
	{
		return NULL;
	}
}

